package com.yiduo.outpost.gateway.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiduo.outpost.core.model.entity.User;
import com.yiduo.outpost.core.model.exception.Asserts;
import com.yiduo.outpost.core.security.UnAuthorizedException;
import com.yiduo.outpost.gateway.mapper.UserMapper;
import com.yiduo.outpost.gateway.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 *
 */
@Service
@CacheConfig(cacheManager = "cacheManager", cacheNames = "UserCache")
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService, UserDetailsService {

    @Autowired
    private PermissionServiceImpl permissionService;

    @Cacheable(key = "#userId")
    public User getUserWithPermission(Long userId) {
        User user = getById(userId);
        Asserts.assertNotNull(user, UnAuthorizedException::new);
//        List<Permission> permissions = new ArrayList<>();
//        if (user.getRole() == UserRole.ROLE_ADMIN) {
//            permissions.addAll(permissionService.selectAll());
//        } else if (user.getPermissionGroupId() != null) {
//            permissions.addAll(permissionService.findByGroupId(user.getPermissionGroupId()));
//        }
//        permissions.addAll(permissionConfig.permissions);
//        user.setPermissions(permissions);
        return user;
    }

    @Override
    public User getUserWithPermissions(Long userId) {
        return null;
    }

    @Override
    public void clearUserCache(Long userId) {

    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        return null;
    }
}
